package com.hyz.service.permission.impl;

import client.AssetClient;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyz.model.entity.User;
import com.hyz.model.dto.ApprovalResultRequest;
import com.hyz.model.vo.AssetSecondmentResponse;
import com.hyz.service.UserService;
import com.hyz.service.permission.ApproveService;
import com.hyz.util.Conversion;
import com.hyz.util.GetUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.ResponseResult;
import pojo.asset.assetSecondment.AssetSecondment;
import pojo.asset.assetSecondment.AssetSecondmentResult;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author HGD
 * @date 2022/10/9 15:25
 */
@Service
public class ApproveServiceImpl implements ApproveService {
    private final int code = 200;
    @Autowired
    private AssetClient assetClient;
    @Autowired
    private UserService userService;
    @Autowired
    private Conversion conversion;
    @Autowired
    private GetUser getUser;

    @Override
    public ResponseResult<List<AssetSecondmentResponse>> apGetAssetSecondments() {

        Long approveId = getUser.getUser().getId();
        User user = userService.getById(approveId);
        if (Objects.isNull(user)) {
            return new ResponseResult<>(400, "查无此人");
        }
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .select(User::getId)
                .eq(User::getDepartmentId, user.getDepartmentId());
        List<User> users = userService.list(wrapper);
        if (users.isEmpty()) {
            return new ResponseResult<>(400, "该部门没有任何员工");
        }
        List<Long> userId = new ArrayList<>();
        for (User user1 : users) {
            userId.add(user1.getId());
        }

        ResponseResult<List<AssetSecondment>> responseResult = assetClient.apGetAssetSecondments(userId);
        if (responseResult.getStatus() != code) {
            return new ResponseResult<>(responseResult.getStatus(), responseResult.getMsg());
        }
        List<AssetSecondment> assetSecondments = responseResult.getData();
        List<AssetSecondmentResponse> assetSecondmentResponse = conversion.getAssetSecondmentResponse(assetSecondments);
        return new ResponseResult<>(200, "获取该部门的所有借用申请", assetSecondmentResponse);
    }

    @Override
    public ResponseResult<List<AssetSecondmentResponse>> apUpdateAssetSecondment(ApprovalResultRequest request) {
        Long assetSecondmentId = request.getApprovalId();
        AssetSecondmentResult result = request.getResult() ? AssetSecondmentResult.APPROVAL_AGREE : AssetSecondmentResult.FAILED;

        ResponseResult<String> responseResult = assetClient.apUpdateAssetSecondment(assetSecondmentId, result);
        if (responseResult.getStatus() != code) {
            return new ResponseResult<>(responseResult.getStatus(), responseResult.getMsg());
        }
        return apGetAssetSecondments();
    }

}
